﻿using System;
using System.Collections.Generic;
using System.Text;
using KaiXin001er.UTIL;
using KaiXin001er.Objects;
using System.Drawing;
using System.Net.Json;
using System.Text.RegularExpressions;
using Microsoft.JScript.Vsa;
using Microsoft.JScript;

namespace KaiXin001er.Logic
{
    public class 页面分析者
    {
        #region 帐户部分
        public static string ReadPlayerID(string src)
        {
            return StringHelper.getMiddleString("title=\"我的开心网ID:", "\">", src);

        }
        public static List<Friend> ReadFriendsFromOnPage(string src)
        {
            List<Friend> list = new List<Friend>();
            int pos = 0;
            string retstr = StringHelper.getMiddleString("<div class=\"l50_s\">", "</div>", src, ref pos);
            while (retstr != null)
            {
                string friendID = StringHelper.getMiddleString("uid=", "\" ", retstr);
                string friendName = StringHelper.getMiddleString("title=\"", "\"", retstr);
                Friend f = new Friend(friendID, friendName);
                list.Add(f);
                retstr = StringHelper.getMiddleString("<div class=\"l50_s\">", "</div>", src, ref pos);
            }
            return list;
        }
        public static string ReadAccountImageURL(string src, string uid)
        {
            string ret = StringHelper.getMiddleString("<a href=\"/photo/logolist.php?uid=" + uid + "\"><img src=\"", "\"", src);
            return ret;
        }
        #endregion
        #region 停车部分
        public static ParkUserData ReadParkUserData(string src)
        {
            ParkUserData pd = new ParkUserData();
            string data = "";
            if (src.IndexOf("var v_userdata =") > 0)
            {
                data = StringHelper.getMiddleString("var v_userdata = ", ";", src);
            }
            else
            {
                data = src;
            }
            JsonTextParser jsontextparser = new JsonTextParser();
            JsonObjectCollection joc = jsontextparser.Parse(data) as JsonObjectCollection;
            if (joc == null) return null;
            ///铁定是5个!
            //error
            JsonObject errorObject = joc["error"];
            pd.Error = errorObject.GetValue() as string;
            //config
            JsonObjectCollection configCollection = joc["config"] as JsonObjectCollection;
            pd.Config = new ParkUserDataConfiger();
            pd.Config.Money_minute = configCollection["money_minute"].GetValue() as string;
            pd.Config.Money_max = configCollection["money_max"].GetValue() as double?;
            //user
            JsonObjectCollection UserCollection = joc["user"] as JsonObjectCollection;
            pd.User = new ParkUserDataUser();
            pd.User.Vuid = UserCollection["vuid"].GetValue() as string;
            pd.User.Uid = UserCollection["uid"].GetValue() as string;
            pd.User.Neighbor = (int?)(UserCollection["neighbor"].GetValue() as double?);
            pd.User.Isfriend = (int?)(UserCollection["isfriend"].GetValue() as double?);
            try
            {
                if (UserCollection["first_fee_parking"] != null)
                    pd.User.First_fee_parking = (int?)(UserCollection["first_fee_parking"].GetValue() as double?);
            }
            catch (Exception) { }
            try
            {
                if (UserCollection["first_free_parking"] != null)
                    pd.User.First_free_parking = (int?)(UserCollection["first_free_parking"].GetValue() as double?);
            }
            catch (Exception) { }
            pd.User.Real_name = UserCollection["real_name"].GetValue() as string;
            pd.User.Ta = UserCollection["ta"].GetValue() as string;
            pd.User.Logo50 = UserCollection["logo50"].GetValue() as string;
            pd.User.Logo20 = UserCollection["logo20"].GetValue() as string;
            pd.User.Avenue = UserCollection["avenue"].GetValue() as string;
            pd.User.Cash = UserCollection["cash"].GetValue() as string;
            pd.User.Scene = UserCollection["scene"].GetValue() as string;
            pd.User.Sceneid = UserCollection["sceneid"].GetValue() as string;
            pd.User.Showmoneyminute = (int?)(UserCollection["showmoneyminute"].GetValue() as double?);
            pd.User.Online2 = (int?)(UserCollection["online2"].GetValue() as double?);

            //parking
            JsonArrayCollection ParkArray = joc["parking"] as JsonArrayCollection;
            pd.Parking = new ParkUserDataParking[ParkArray.Count];
            for (int i = 0; i < pd.Parking.Length; i++)
            {

                JsonObjectCollection parkCollection = ParkArray[i] as JsonObjectCollection;
                pd.Parking[i] = new ParkUserDataParking();
                pd.Parking[i].Parkid = parkCollection["parkid"].GetValue() as string;
                pd.Parking[i].Car_uid = parkCollection["car_uid"].GetValue() as string;
                pd.Parking[i].Carid = parkCollection["carid"].GetValue() as string;
                pd.Parking[i].Ctime = (int?)(parkCollection["ctime"].GetValue() as double?);
                pd.Parking[i].Car_real_name = parkCollection["car_real_name"].GetValue() as string;
                pd.Parking[i].Car_logo20 = parkCollection["car_logo20"].GetValue() as string;
                pd.Parking[i].Car_isfriend = (int?)(parkCollection["car_isfriend"].GetValue() as double?);
                pd.Parking[i].Car_logo_big = parkCollection["car_logo_big"].GetValue() as string;
                pd.Parking[i].Car_logo_small = parkCollection["car_logo_small"].GetValue() as string;
                pd.Parking[i].Car_logo_flash = parkCollection["car_logo_flash"].GetValue() as string;
                pd.Parking[i].Car_police = parkCollection["car_police"].GetValue() as string;
                pd.Parking[i].Car_profit = (int?)(parkCollection["car_profit"].GetValue() as double?);
                pd.Parking[i].Car_trademark = parkCollection["car_trademark"].GetValue() as string;
                pd.Parking[i].Car_trademarkname = parkCollection["car_trademarkname"].GetValue() as string;
                pd.Parking[i].Car_price = parkCollection["car_price"].GetValue() as string;
                pd.Parking[i].Car_name = parkCollection["car_name"].GetValue() as string;
            }
            //car
            JsonArrayCollection CarArray = joc["car"] as JsonArrayCollection;
            if (CarArray != null)
            {
                pd.Car = new ParkUserDataCar[CarArray.Count];
                for (int i = 0; i < pd.Car.Length; i++)
                {
                    JsonObjectCollection carcollection = CarArray[i] as JsonObjectCollection;
                    pd.Car[i] = new ParkUserDataCar();
                    pd.Car[i].Carid = carcollection["carid"].GetValue() as string;
                    pd.Car[i].Price = carcollection["price"].GetValue() as string;
                    pd.Car[i].Neighbor = carcollection["neighbor"].GetValue() as string;
                    pd.Car[i].Park_uid = carcollection["park_uid"].GetValue() as string;
                    pd.Car[i].Parkid = carcollection["parkid"].GetValue() as string;
                    pd.Car[i].Ctime = (int?)(carcollection["ctime"].GetValue() as double?);
                    pd.Car[i].Ctype = carcollection["ctype"].GetValue() as string;
                    pd.Car[i].Car_name = carcollection["car_name"].GetValue() as string;
                    pd.Car[i].Car_logo_big = carcollection["car_logo_big"].GetValue() as string;
                    pd.Car[i].Car_logo_small = carcollection["car_logo_small"].GetValue() as string;
                    pd.Car[i].Car_logo_flash = carcollection["car_logo_flash"].GetValue() as string;
                    pd.Car[i].Car_trademark = carcollection["car_trademark"].GetValue() as string;
                    pd.Car[i].Car_trademarkname = carcollection["car_trademarkname"].GetValue() as string;
                    pd.Car[i].Car_police = carcollection["car_police"].GetValue() as string;
                    pd.Car[i].Park_real_name = carcollection["park_real_name"].GetValue() as string;
                    pd.Car[i].Park_logo20 = carcollection["park_logo20"].GetValue() as string;
                    pd.Car[i].Park_profit = (int?)(carcollection["park_profit"].GetValue() as double?);
                    pd.Car[i].Park_moneyminute = (int?)(carcollection["park_moneyminute"].GetValue() as double?);
                }
            }
            return pd;

        }

        public static ParkFriendData[] ReadParkFriendData(string src)
        {
            string data = StringHelper.getMiddleString("var v_frienddata = ", ";", src);
            JsonTextParser jtp = new JsonTextParser();
            JsonArrayCollection jac = jtp.Parse(data) as JsonArrayCollection;
            ParkFriendData[] pfd = new ParkFriendData[jac.Count];
            for (int i = 0; i < pfd.Length; i++)
            {
                JsonObjectCollection joc = jac[i] as JsonObjectCollection;
                pfd[i] = new ParkFriendData();
                pfd[i].Uid = joc["uid"].GetValue() as string;
                pfd[i].Real_name = joc["real_name"].GetValue() as string;
                pfd[i].Ta = joc["ta"].GetValue() as string;
                pfd[i].Full = joc["full"].GetValue() as string;
                pfd[i].Scenemoney = joc["scenemoney"].GetValue() as string;
                pfd[i].Scenename = joc["scenename"].GetValue() as string;
                pfd[i].Neighbor = (int?)(joc["neighbor"].GetValue() as double?);
                pfd[i].Online = (int?)(joc["online"].GetValue() as double?);
            }
            return pfd;

        }
        public static string 争车位g_verify(string src)
        {
            return StringHelper.getMiddleString("var g_verify = \"", "\";", src);
        }
        public static ParkResult 停车结果(string src)
        {
            ParkResult pr = new ParkResult();
            JsonTextParser jtp = new JsonTextParser();
            JsonObjectCollection Joc = jtp.Parse(src) as JsonObjectCollection;
            pr.Cash = (int?)(Joc["cash"].GetValue() as double?);
            pr.Neighbor = (int?)(Joc["neighbor"].GetValue() as double?);
            pr.Park_uid = (int?)(Joc["park_uid"].GetValue() as double?);
            pr.Parkid = (int?)(Joc["parkid"].GetValue() as double?);
            pr.Carid = (int?)(Joc["carid"].GetValue() as double?);
            pr.Ctime = (int?)(Joc["ctime"].GetValue() as double?);
            pr.Moneyminute = Joc["moneyminute"].GetValue() as string;
            pr.Errno = (int?)(Joc["errno"].GetValue() as double?);
            pr.Error = Joc["error"].GetValue() as string;
            return pr;


        }
        public static string 计算Acc(string src)
        {
            string Pattern = "";
            string script = "";
            string script_tmp2 = "";
            string var_values = "";
            Pattern = "<script.*?>(?<x>.*?)</script>";
            MatchCollection scripts = Regex.Matches(src, Pattern, RegexOptions.Singleline);
            Pattern = @"function\s*?acc";
            foreach (System.Text.RegularExpressions.Match script_tmp in scripts)
            {
                if (Regex.IsMatch(script_tmp.Groups["x"].ToString(), Pattern, RegexOptions.Singleline))
                {
                    script = script_tmp.Groups["x"].ToString();
                }
            }
            if (script.Length == 0)
            {

                return script;
            }
            script_tmp2 = script;
            Pattern = "function.*?[^{]*?{[^{}]*(((?'Open'{)[^{}]*)+((?'-Open'})[^{}]*)+)*(?(Open)(?!))}";
            script_tmp2 = Regex.Replace(script, Pattern, "", RegexOptions.Singleline);
            Pattern = "var.*?=.*?\".*?\".*?;";
            foreach (System.Text.RegularExpressions.Match var_value in Regex.Matches(script_tmp2, Pattern, RegexOptions.Multiline))
            {
                var_values = var_values + var_value.ToString();
            }
            Pattern = @"function\s*?acc[^{]*?{[^{}]*(((?'Open'{)[^{}]*)+((?'-Open'})[^{}]*)+)*(?(Open)(?!))}";
            System.Text.RegularExpressions.Match acc_f = Regex.Match(script, Pattern, RegexOptions.Singleline);
            return runJs(var_values + acc_f.ToString() + "acc();");
        }
        private static string runJs(string scriptText)
        {
            VsaEngine ve = VsaEngine.CreateEngine();
            return Eval.JScriptEvaluate(scriptText, ve).ToString();
        }
        #endregion

        #region 拉力赛部分
        public static MatchInfo[] 拉力赛事列表(string src)
        {
            List<MatchInfo> minfos = new List<MatchInfo>();
            int pos = 0;
            string MatchString = StringHelper.getMiddleString("<div id=\"matchinglist\" style=\"display:none;\">", "<div id=\"overmatchlist\" style=\"display:none;\">", src, ref pos);
            pos = 0;
            string theFirstUserString = "";
            while (theFirstUserString != null)
            {
                int pos2 = 0;
                theFirstUserString = StringHelper.getMiddleString("<div class=\"l\" style=\"width:650px;\">", "<img src=\"http://img.kaixin001.com.cn/i2/park/match/ico_vs.gif\"", MatchString, ref pos);
                if (theFirstUserString == null) continue;//如果取到为空 直接跳入下一个循环
                MatchInfo minfo = new MatchInfo();
                //取第一个好友的字符串 如果多个 还得循环取出多个
                string uid = StringHelper.getMiddleString("javascript:team(", " , '", theFirstUserString, ref pos2);
                minfo.FirstUserIDs.Add(uid);
                string theFirestUser = StringHelper.getMiddleString("');\">", "</a>", theFirstUserString, ref pos2);
                string tempLastString = StringHelper.getLastString(theFirstUserString, pos2 + 1);
                while (true)
                {
                    if (StringHelper.Contains("<a class=sl href=\"", tempLastString))//如果有下一个好友
                    {
                        uid = StringHelper.getMiddleString("javascript:team(", " , '", theFirstUserString, ref pos2);
                        minfo.FirstUserIDs.Add(uid);
                        theFirestUser += " 和 " + StringHelper.getMiddleString("');\">", "</a>", theFirstUserString, ref pos2);
                    }
                    else
                    {
                        break;//没有退出
                    }
                    tempLastString = StringHelper.getLastString(theFirstUserString, pos2 + 1);
                }
                theFirestUser += tempLastString;

                pos2 = 0;
                string theSecondUserString = StringHelper.getMiddleString("align=\"absmiddle\" />", "</div>", MatchString, ref pos);
                //取第一个好友的字符串 如果多个 还得循环取出多个
                uid = StringHelper.getMiddleString("javascript:team(", " , '", theSecondUserString, ref pos2);
                minfo.SecondUserIDs.Add(uid);
                string theSecondUser = StringHelper.getMiddleString("');\">", "</a>", theSecondUserString, ref pos2);

                tempLastString = StringHelper.getLastString(theSecondUserString, pos2 + 1);
                while (true)
                {
                    if (StringHelper.Contains("<a class=sl href=\"", tempLastString))//如果有下一个好友
                    {
                        uid = StringHelper.getMiddleString("javascript:team(", " , '", theSecondUserString, ref pos2);
                        minfo.SecondUserIDs.Add(uid);
                        theSecondUser += " 和 " + StringHelper.getMiddleString("');\">", "</a>", theSecondUserString, ref pos2);
                    }
                    else
                    {
                        break;//没有退出
                    }
                    tempLastString = StringHelper.getLastString(theSecondUserString, pos2 + 1);
                }
                theSecondUser += tempLastString;

                string daysString = StringHelper.getMiddleString("<div class=\"l c6\" style=\"width:50px;\">", "</div>", MatchString, ref pos);
                string MatchDetailString = StringHelper.getMiddleString("<div class=\"l\" style=\"width:50px;\">", "</div>", MatchString, ref pos);

                string UidAndID = StringHelper.getMiddleString("javascript:matchDetail(", ");\">查看赛况", MatchDetailString);
                string[] ids = UidAndID.Split(',');
                string matchUid = ids[0].Trim();
                string matchID = ids[1].Trim();


                minfo.FirstUserString = theFirestUser;
                minfo.SecondUserString = theSecondUser;
                minfo.DayString = daysString;
                minfo.MatchUid = matchUid;
                minfo.MatchID = matchID;
                minfos.Add(minfo);
            }
            return minfos.ToArray();
        }
        public static int 拉力赛提示code(string src)
        {
            if (src == null)
            {
                throw new ArgumentNullException("url");
            }
            int code = -1;
            string a = "action=";
            int index = src.IndexOf(a);
            if (index >= 0)
            {
                src = src.Substring(index + a.Length);
                StringBuilder buffer = new StringBuilder();
                int length = src.Length;
                for (int i = 0; i < length; i++)
                {
                    if (char.IsDigit(src[i]))
                    {
                        buffer.Append(src[i]);
                    }
                    else
                    {
                        break;
                    }
                }
                if (buffer.Length > 0)
                {
                    code = int.Parse(buffer.ToString());
                }
            }
            return code;

        }
        public static string 拉力赛提示信息(string src, int code)
        {
            string ret = "";
            string tempstring = StringHelper.getMiddleString("<div id=action" + code.ToString() + " style=\"display:block\">", "<div class=\"c\"></div>", src);
            if (tempstring != null)
            {
                ret = StringHelper.getMiddleString("<strong>", "</strong>", tempstring);
                if (ret == null) ret = "";
            }
            return ret;
        }
        #endregion

        #region 花园部分
        public static SeedAndFurit[] 种子列表(string seedstr)
        {
            List<SeedAndFurit> seeds = new List<SeedAndFurit>();
            int pos = 0;
            while (true)///循环读取种子列表
            {
                string itemstr = StringHelper.getMiddleString("<item>", "</item>", seedstr, ref pos);//每一个item
                if (itemstr == null) break;
                string name = StringHelper.getMiddleString("<name>", "</name>", itemstr);
                string fruitpic = StringHelper.getMiddleString("<fruitpic>", "</fruitpic>", itemstr);
                string seedid = StringHelper.getMiddleString("<seedid>", "</seedid>", itemstr);
                string price = StringHelper.getMiddleString("<price>", "</price>", itemstr);
                SeedAndFurit seed = new SeedAndFurit();
                seed.Name = name;
                seed.Fruitpic = fruitpic;
                seed.Seedid = seedid;
                seed.Price = price;
                seeds.Add(seed);
            }
            return seeds.ToArray();
        }
        public static string 花园verify(string src)
        {
            return StringHelper.getMiddleString("var g_verify = \"","\";",src);
        }
        public static GardenUserInfo 花园用户信息(string src)
        {
            int pos = 0;
            GardenUserInfo g = new GardenUserInfo();
            g.Rank = StringHelper.ReadXmlSchema("rank", src, ref pos);
            g.Ranktip=StringHelper.ReadXmlSchema("ranktip",src,ref pos);
            g.Name=StringHelper.ReadXmlSchema("name",src,ref pos);
            g.Cashtip=StringHelper.ReadXmlSchema("cashtip",src,ref pos);
            g.Tcharms=StringHelper.ReadXmlSchema("tcharms",src , ref pos);
            g.Flowerwomanurl=StringHelper.ReadXmlSchema("flowerwomanurl",src,ref pos);

            string gardenstr = StringHelper.ReadXmlSchema("garden", src, ref pos);
            int gpos = 0;
            List<GardenItem> list = new List<GardenItem>();
            while (true)
            {
                string itemstr = StringHelper.ReadXmlSchema("item",gardenstr,ref gpos);
                int ipos = 0;
                if (itemstr == null) break;
                GardenItem gi = new GardenItem();
                gi.Water = StringHelper.ReadXmlSchema("water", itemstr, ref ipos);
                gi.Farmnum = StringHelper.ReadXmlSchema("farmnum", itemstr, ref ipos);
                gi.Vermin = StringHelper.ReadXmlSchema("vermin", itemstr, ref ipos);
                gi.Cropsid = StringHelper.ReadXmlSchema("cropsid", itemstr, ref ipos);
                gi.Fuid = StringHelper.ReadXmlSchema("fuid", itemstr, ref ipos);
                gi.Status = StringHelper.ReadXmlSchema("status", itemstr, ref ipos);
                gi.Grass = StringHelper.ReadXmlSchema("grass", itemstr, ref ipos);
                gi.Shared = StringHelper.ReadXmlSchema("shared", itemstr, ref ipos);
                gi.Pic = StringHelper.ReadXmlSchema("pic", itemstr, ref ipos);
                gi.Fruitpic = StringHelper.ReadXmlSchema("fruitpic", itemstr, ref ipos);
                gi.Picheight = StringHelper.ReadXmlSchema("picwidth", itemstr, ref ipos);
                gi.Picwidth = StringHelper.ReadXmlSchema("picheight", itemstr, ref ipos);
                gi.Cropsstatus = StringHelper.ReadXmlSchema("cropsstatus", itemstr, ref ipos);
                gi.Grow = StringHelper.ReadXmlSchema("grow", itemstr, ref ipos);
                gi.Totalgrow = StringHelper.ReadXmlSchema("totalgrow", itemstr, ref ipos);
                gi.Fruitnum = StringHelper.ReadXmlSchema("fruitnum", itemstr, ref ipos);
                gi.Seedid = StringHelper.ReadXmlSchema("seedid", itemstr, ref ipos);
                gi.Name = StringHelper.ReadXmlSchema("name", itemstr, ref ipos);
                string cropsstring = StringHelper.ReadXmlSchema("crops", itemstr, ref ipos);
                if (cropsstring != null)
                {
                    gi.Crops = cropsstring.Replace("&lt;br&gt;&lt;font size='12' color='#666666'&gt;", "").Replace("&lt;/font&gt;&lt;br&gt;", "").Replace("&lt;br&gt;", "个");
                }
                string farmstring = StringHelper.ReadXmlSchema("farm", itemstr, ref ipos);
                if (farmstring != null)
                {
                    gi.Farm = farmstring.Replace("&lt;br&gt;", gi.Water).Replace("&lt;br&gt;", ""); ;
                }

                list.Add(gi);
            }
            g.GardenItem = list.ToArray();

            return g;
        }
        public static SmartC 花园精灵信息(string src)
        {
            SmartC sc = new SmartC();
            int pos=0;
            string faqString = StringHelper.ReadXmlSchema("faq",src,ref pos);
            List<SmartItem> list = new List<SmartItem>();
            int posl = 0;
            while (true)
            {

                SmartItem si = new SmartItem();
                si.Pic = StringHelper.ReadXmlSchema("pic", faqString, ref posl);
                if (si.Pic == null) break;
                si.Id = StringHelper.ReadXmlSchema("id", faqString, ref posl);
                list.Add(si);
            }
            sc.Faq = list.ToArray();
            sc.Tpic = StringHelper.ReadXmlSchema("tpic", src, ref pos);
            sc.Ret = StringHelper.ReadXmlSchema("ret",src,ref pos);
            return sc;
        }
        #endregion

        #region 牧场部分
        public static RanchUserInfo 牧场用户信息(string config)
        {
            RanchUserInfo rui = new RanchUserInfo();
            int pos = 0;
            List<RanchAnimal> ral = new List<RanchAnimal>();

            ///读取动物
            string animalsString = StringHelper.ReadXmlSchema("animals", config, ref pos);
            int anpos = 0;
            while (true)
            {

                string uid = StringHelper.ReadXmlSchema("uid", animalsString, ref anpos);
                if (uid == null) break;
                RanchAnimal ra = new RanchAnimal();
                ra.Uid = uid;
                ra.Animalsid = StringHelper.ReadXmlSchema("animalsid", animalsString, ref anpos);
                ra.Aid = StringHelper.ReadXmlSchema("aid", animalsString, ref anpos);
                ra.Status = StringHelper.ReadXmlSchema("status", animalsString, ref anpos);
                ra.Tips = StringHelper.ReadXmlSchema("tips", animalsString, ref anpos);
                ra.Skey = StringHelper.ReadXmlSchema("skey", animalsString, ref anpos);
                ra.Aname = StringHelper.ReadXmlSchema("aname", animalsString, ref anpos);
                ra.Paction = StringHelper.ReadXmlSchema("paction", animalsString, ref anpos);
                ral.Add(ra);
            }
            rui.RanchAnimals = ral.ToArray();

            ///读取食物
            List<RanchFood> rfl = new List<RanchFood>();
            string rachFoodString = StringHelper.ReadXmlSchema("foods", config, ref pos);
            int ranchfoolpos = 0;
            while (true)
            {
                string tips = StringHelper.ReadXmlSchema("tips", rachFoodString, ref ranchfoolpos);
                if (tips == null) break;
                RanchFood rf = new RanchFood();
                rf.Tips = tips;
                rf.Seedid = StringHelper.ReadXmlSchema("seedid", rachFoodString, ref ranchfoolpos);
                rf.Grass = StringHelper.ReadXmlSchema("grass", rachFoodString, ref ranchfoolpos);
                rfl.Add(rf);
            }
            rui.RanchFoods = rfl.ToArray();


            ///读取产品
            List<RanchProduct2> rpl = new List<RanchProduct2>();
            string productString = StringHelper.ReadXmlSchema("product2", config);
            int productPos = 0;
            while (true)
            {
                string itemstring = StringHelper.ReadXmlSchema("item",productString,ref productPos);
                if (itemstring == null) break;
                RanchProduct2 p = new RanchProduct2();
                p.Uid = StringHelper.ReadXmlSchema("uid", itemstring);
                p.Aid = StringHelper.ReadXmlSchema("aid", itemstring);
                p.Type = StringHelper.ReadXmlSchema("type", itemstring);
                p.Num = StringHelper.ReadXmlSchema("num", itemstring);
                p.Stealnum = StringHelper.ReadXmlSchema("stealnum", itemstring);
                p.Mtime = StringHelper.ReadXmlSchema("mtime", itemstring);
                p.Ppic = StringHelper.ReadXmlSchema("ppic", itemstring);
                p.Tname = StringHelper.ReadXmlSchema("tname", itemstring);
                p.Skey = StringHelper.ReadXmlSchema("skey", itemstring);
                p.Pname = StringHelper.ReadXmlSchema("pname", itemstring);
                p.Tips = StringHelper.ReadXmlSchema("tips", itemstring);
                p.Oa = StringHelper.ReadXmlSchema("oa", itemstring);
                rpl.Add(p);
            }

            rui.RanchProducts = rpl.ToArray();

            rui.Water = StringHelper.ReadXmlSchema("water", config, ref pos);
            if (rui.Water == "") rui.Water = "0";
            rui.Watertips = StringHelper.ReadXmlSchema("watertips", config, ref pos);
            rui.Grass = StringHelper.ReadXmlSchema("grass", config, ref pos);
            rui.Grasstips = StringHelper.ReadXmlSchema("grasstips", config, ref pos);

            return rui;

        }
        public static Animal[] 动物列表(string src)
        {
            List<Animal> list = new List<Animal>();
            int pos = 0;
            while (true)
            {
                string itemstring = StringHelper.getMiddleString("<item>", "</item>", src, ref pos);
                if (itemstring == null) break;
                string name = StringHelper.getMiddleString("<name>", "</name>", itemstring);
                string pic = StringHelper.getMiddleString("<pic>", "</pic>", itemstring);
                string aid = StringHelper.getMiddleString("<aid>", "</aid>", itemstring);
                string skey = StringHelper.getMiddleString("<skey>", "</skey>", itemstring);
                string price = StringHelper.getMiddleString("<price>", "</price>", itemstring);

                Animal animal = new Animal();
                animal.Name = name;
                animal.Pic = pic;
                animal.Aid = aid;
                animal.Skey = skey;
                animal.Price = price;
                list.Add(animal);
            }
            return list.ToArray();
        }
        #endregion
        #region 打工部分
        public static string 最好的工作(string src)
        {
            string petid = "";
            string price = "";
            int pos = 0;
            int iprice=0;
            string retpetid = "";
            while (petid != null)
            {
                petid = StringHelper.getMiddleString("type=\"radio\" value=\"", "\" />", src, ref pos);
                if (petid == null) continue;
                price = StringHelper.getMiddleString("工资：", "元/天",src,ref pos);
                int temp = int.Parse(price);
                if (temp > iprice)
                {
                    iprice = temp;
                    retpetid = petid;
                }
            }
            return retpetid;
        }
        #endregion

        #region 朋友买卖部分
        public static 朋友买卖信息 读取朋友买卖信息(string str)
        {

            朋友买卖信息 信息 = new 朋友买卖信息();
            int pos=0;
            信息.我是谁的奴隶 = StringHelper.getMiddleString("<span class=\"f12 c9\">", "</span>", str, ref pos);
            信息.身价 = StringHelper.getMiddleString("<p>身　价：<strong class=\"dgreen\">&yen;", "</strong></p>", str, ref pos);
            信息.总资产 = StringHelper.getMiddleString("<p>总资产：<strong class=\"dgreen\">&yen;", "</strong></p>", str, ref pos);
            信息.现金 = StringHelper.getMiddleString("<p>现　金：<strong class=\"dgreen\">&yen;", "</strong></p>",str, ref pos);
            信息.奴隶数 = StringHelper.getMiddleString("<p>奴隶数：<strong class=\"dred\">", "</strong></p>", str, ref pos);

            List<奴隶信息> l = new List<奴隶信息>(); 
            while (true)
            {
                string id = StringHelper.getMiddleString("<div class=\"l\"><strong><a href=\"/!slave/index.php?uid=", "\" class", str, ref pos);
                if (id == null) break;
                奴隶信息 奴隶 = new 奴隶信息();
                奴隶.ID = id;
                奴隶.名字 = StringHelper.getMiddleString("\"sl2\">", "</a></strong>", str, ref pos);
                奴隶.身价 = StringHelper.getMiddleString("身　价：<strong class=\"dgreen\">&yen;", "</strong><br/>", str, ref pos);
                奴隶.总资产 = StringHelper.getMiddleString("总资产：<strong class=\"dgreen\">&yen;", "</strong><br/>", str, ref pos);
                奴隶.现金 = StringHelper.getMiddleString("现　金：<strong class=\"dgreen\">&yen;", "</strong><br/>", str, ref pos);
                奴隶.奴隶数 = StringHelper.getMiddleString("奴隶数：<strong class=\"dred\">", "</strong><br/>",str,ref pos);
                l.Add(奴隶);

            }
            信息.奴隶 = l;
            return 信息;
        }

        public static 安抚列表 读取安抚列表(string str)
        {
            安抚列表 list = new 安抚列表();
            int pos = 0;

            while (true)
            {
                string value = StringHelper.getMiddleString("<li><input type=\"radio\" name=\"comforttype\" value=\"", "\"", str, ref pos);
                if(value==null)break;
                string desc=StringHelper.getMiddleString(">","</li>",str,ref pos);
                安抚信息 info = new 安抚信息();
                info.Value = value;
                info.Desc = desc;
                list.Add(info);
            }
            return list;
        }
        #endregion

    }
}
